package com.online.onlineshop.services.impl;

import com.online.onlineshop.entity.User;
import com.online.onlineshop.mapper.UserMapper;
import com.online.onlineshop.services.ForUserService;
import com.online.onlineshop.services.exce.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/*
 * 用户服务接口的实现类
 *
 * */
@Service
public class ForUserServiceImpl implements ForUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    HttpSession session;

    //默认头像
    private static String originalPortrait = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01bcc455bee3b66ac7253f3650ef81.jpg%402o.jpg&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648715799&t=1711497e394b95369ed5c1fd311dca3f";

    //修改密码的方法
    @Override
    public void updatePassword(Integer uid, String oldPassword, String newPassword) {
        User result = userMapper.selectById(uid);
        String dbPassword = result.getPassword();
        if (result == null) {
            throw new UserNameNotFoundException("用户数据未找到");
        }
        if (!oldPassword.equals(dbPassword)) {
            throw new PassWordNotFoundException("密码错误");
        }
        User user = new User();
        user.setUid(uid);
        user.setPassword(newPassword);
        int i = userMapper.updateById(user);
        if (i != 1) {
            throw new UpdateException("更新密码时产生错误");
        }
    }

    @Override
    public User login(String username, String password) {
        HashMap<String, Object> lUser = new HashMap<>();
        lUser.put("username", username);
        lUser.put("password", password);
        List<User> users = userMapper.selectByMap(lUser);
        if (users.isEmpty() == true) {
            throw new UserNameNotFoundException("用户未存在");
        }
        String bPassword = users.get(0).getPassword();
        int isDeleted = users.get(0).getIsDeleted();

        if (!bPassword.equals(password)) {
            throw new PassWordNotFoundException("密码错误");
        }
        if (isDeleted == 1) {
            throw new UserNameNotFoundException("该用户已被注销");
        }
        User rUser = users.get(0);
        //将用户数据返回前端
        return rUser;
    }


    @Override
    public void register(User user) {
        HashMap<String, Object> UserHashMap = new HashMap<>();
        UserHashMap.put("username", user.getUsername());
        UserHashMap.put("password", user.getPassword());
        //通过以下方法查看用户名是否已存在
        List<User> resultUsers = userMapper.selectByMap(UserHashMap);


        if (!resultUsers.isEmpty()) {
            throw new UserNameOccupiedExceoption("用户名被占用");
        }
        /* 初始化用户数据*/
        Date date = new Date();
        user.setIsDeleted(0);
        user.setUserType(user.getUserType());
        user.setCreatedTime(date);
        user.setCreatedUser(user.getUsername());
        user.setModifiedTime(date);
        user.setModifiedUser(user.getUsername());
        user.setPortrait(originalPortrait);
        //用户进行注册操作
        int insertFeedBack = userMapper.insert(user);
        if (insertFeedBack != 1) {
            throw new InsertingException("用户在注册时产生未知异常");
        }
    }


    @Override
    public User getByUid(Integer uid) {
        User user = userMapper.selectById(uid);
        if (user == null || user.getIsDeleted() == 1) {
            throw new UserNameNotFoundException("用户未找到");
        }
        return user;
    }

    @Override
    public void updateInfo(Integer uid, String username, User user) {
        User result = userMapper.selectById(uid);
        if (result == null) {
            throw new UpdateException("未找到要修改信息的用户");
        }
        user.setUid(uid);
        user.setUsername(username);
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(new Date());
        int i = userMapper.updateById(user);
        if (i == 0) {
            throw new UpdateException("修改信息失败");
        }

    }

    @Override
    public void changePortrait(Integer uid, String portrait, String userName) {
        User user = userMapper.selectById(uid);
        if (user == null || user.getIsDeleted() == 1) {
            throw new UserNameNotFoundException("用户未发现");
        }
        user.setPortrait(portrait);
        user.setModifiedUser(userName);
        user.setModifiedTime(new Date());
        int i = userMapper.updateById(user);

        if (i != 1) {
            throw new UpdateException("头像更新异常");
        }

    }
}
